﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AmiEmuLib.Configuration;
using System.Reflection;
using System.Xml;
using System.Diagnostics;
using AmiEmuLib.AmigaConfiguration;

namespace AmiEmuLib.AmigaMachine.AmigaMemory
{
    /// <summary>
    /// Represent 64KB ram memory bank.
    /// </summary>
    public class RamMemoryBank : MemoryBank
    {
        /// <summary>
        /// 64KB memory.
        /// </summary>
        protected ushort[] m_values = new ushort[Consts.BANK_SIZE / 2];

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="a_address"></param>
        /// <param name="a_memory"></param>
        /// <param name="a_type,"></param>
        /// <param name="a_fast"></param>
        public RamMemoryBank(uint a_address, Memory a_memory, MemoryBankType a_type, bool a_fast)
            : base(a_address, a_memory, a_type, a_fast)
        {
        }

        internal override void Reset()
        {
            for (int i = 0; i < Consts.BANK_SIZE / 2; i++)
                m_values[i] = Consts.RESET_WORD_VALUE;
        }

        public override byte GetByte(uint a_address)
        {
            if ((a_address & 1) == 0)
                return MemoryHelpers.GetHighByte(m_values[GetOffset(a_address)]);
            else
                return MemoryHelpers.GetLowByte(m_values[GetOffset(a_address)]);
        }

        public override void SetByte(uint a_address, byte a_value)
        {
            if ((a_address & 1) == 0)
                MemoryHelpers.SetHighByte(ref m_values[GetOffset(a_address)], a_value);
            else
                MemoryHelpers.SetLowByte(ref m_values[GetOffset(a_address)], a_value);
        }

        public override ushort GetWord(uint a_address)
        {
            return m_values[GetOffset(a_address)];
        }

        public override void SetWord(uint a_address, ushort a_value)
        {
            m_values[GetOffset(a_address)] = a_value;
        }

        public override uint GetLong(uint a_address)
        {
            return MemoryHelpers.MakeLong(GetWord(a_address), GetWord(a_address + 2));
        }

        public override void SetLong(uint a_address, uint a_value)
        {
            SetWord(a_address, MemoryHelpers.GetHighWord(a_value));
            SetWord(a_address + 2, MemoryHelpers.GetLowWord(a_value));
        }
    }
}
